﻿

import Request from '../../base/request.js?t=202511041006'
import Storage from '../../base/storage.js?t=202511041006'
import { imageBaseUrl, IgetUserList, getBrowserInfo } from "../../base/utils.js?t=202511041006"
const Composition = (proxy) => {
    const data = reactive({
        proxy: "",
        messageTest: "",
        formState: {
            DepartmentName: null,
            ScoreRate: null,
            DepartmentTypeId: null,
            Type_1_UserList:[]
        },

        deptList:[],
        pagination: {
            total: 0,
            loading:true,
            current: 1,
            pageSize: 50,
            showTotal: total => `总共 ${total} 项`,
            defaultPageSize: 50,
            // pageSizeOptions: ['5', '10', '15', '20'], // 可不设置使用默认
            showSizeChanger: true, // 是否显示pagesize选择
            showQuickJumper: false, // 是否显示跳转窗
        },

        userList:[],
        rules: {
            DepartmentTypeId: [{ required: true, message: '请选择办公类型', trigger: 'change' }],
            ScoreRate: [{ required: true, message: '请填写扣分系数', trigger: 'change' }],

        },
        opType: "",
      

        DepartmentTypeList:[],
        open: false,

        queryParams: {
            dept: null,
        },
 
        workTypeList:[
       

        ],

        columns: [
            {
                title: '序号',
                dataIndex: 'index',
                key: 'index',
                align: 'center',
                width: 80,
                customRender: ({ index }) => {
                    return (params.pagination.value.current - 1) * params.pagination.value.pageSize + index + 1
                },
            },
            {
                title: '部门名称 ',
                dataIndex: 'DepartmentName',
                key: 'DepartmentName',
                align: 'center'

            },
            {
                title: '办公类型',
                dataIndex: 'DepartmentTypeName',
                key: 'DepartmentTypeName',
                align: 'center',
                customRender: ({ text }) => {
                    return text
                },
            },
            {
                title: '扣分系数',
                dataIndex: 'ScoreRate',
                key: 'ScoreRate',
                align: 'center'
            },
   
            {
                title: '操作',
                key: 'action',
                align: 'center',
                customRender: function ({ text, record, index, column }) {
                    return h("div", {
                    },
                        [h(antd.Button, {
                            style: 'margin-right:4px',
                            type: "link",
                            onClick: () => { methods.handleDialog(1, record.Id) }
                        }, { default: () => '编辑' })]
                    );



                }
            },
        ],
        dataSource: [],

    })
    let params = toRefs(data)

    const methods = {

        filterOption: (val, type) => {
            console.log(val, type)
            return `${type.label}`.includes(`${val || ''}`)
        },
        queryList: () => {
      
            methods.getTableList(true)
       
        },
        GetDepartmentTypeList: () => {
            Request({
                url: "/performance/GetDepartmentTypeList",
                method: 'post',
     
            }).then(res => {
                params.DepartmentTypeList.value = res.Data.map(e => {
                    return {
                        label: e.TypeName,
                        value: e.Id
                    }
                })
            })
        },
        handleTableChange: (page) => {
            const { current, pageSize } = page
            params.pagination.value.current = current;
            params.pagination.value.pageSize = pageSize;
            methods.getTableList()
        },

        handleDialog: (type, id) => {


            params.opType.value = type

            if (type == 0) {
                //新增
                params.proxy.value.$refs.formRef && params.proxy.value.$refs.formRef.resetFields();
            }
            else {
       

                let res = params.dataSource.value && params.dataSource.value.find(e => e.Id == id)

                console.log(res.Type_1_UserList)

                params.formState.value=res
                //修改

            }
            //opType  0 新增  1 修改 2详情
            params.open.value = true
        },


        getTableList: (isRresh) => {
            if (isRresh) {
                params.pagination.value.current = 1
            }
            params.pagination.value.loading = true
            Request({
                url: "/performance/GetDepartmentList",
                method: 'post',
                params: {
                    page: params.pagination.value.current,
                    pageSize: params.pagination.value.pageSize,
                    Name: params.queryParams.value.dept
                },
            }).then(res => {
                params.dataSource.value = res.Data
                params.pagination.value.total = res.Data.length
                params.pagination.value.loading = false
/*                params.messageTest.value.success(res.Msg);*/
            })

        },


        init: () => {
              getBrowserInfo().catch(err => { console.log(err)})
            if (proxy) {
                params.proxy.value = proxy
            }
            const { message } = antd
            params.messageTest.value = message
            params.messageTest.value.config({
                top: `10vh`,
                duration: 7,
                maxCount: 1,
                rtl: true,
            });

            let info = Storage.get('AccountInfo', true)
            Request({
                url: "/Account/createJwtToken",
                method: 'post',
                data: {
                    "userName": info && info.UserNo
                }
            }).then(async res => {
                Storage.set('token', res.Data,true)
                methods.getTableList(true)

                methods.GetDepartmentTypeList()


                params.userList.value = await IgetUserList(Request)





                Request({
                    url: "/performance/getperformanceconfig",
                    method: 'post',
                    params: {
                        TypeId:5
                    }
                }).then(res => {

                    params.workTypeList.value = res.Data && res.Data.map(e => {
                        return {
                           label:e.ConfigName,
                            value: e.Id,
                        }
                    })
                })
                Request({
                    url: "/SystemConfig/GetDepartmentList",
                    method: 'post',
                }).then(res => {
                    params.deptList.value = res.Data.map(e => {
                        return {
                            label: e.DepartmentName,
                            value: e.Id,

                        }
                    })
      
                })

                   
            })

        },

 

  
        handleOk: () => {

 
            params.proxy.value.$refs.formRef.validate()
                .then(() => {

                    Request({
                        url: "/performance/UpdateDepartmentConfig",
                        method: 'post',
                        data: {
                            ...params.formState.value,
                        } 
                    }).then(res => {
                        params.messageTest.value.success(res.Msg);
                        params.proxy.value.$refs.formRef && params.proxy.value.$refs.formRef.resetFields();
                        methods.getTableList(true)
                    }).finally(() => {
                        params.open.value = false
                    })
                }).catch(error => {
                    if (error.errorFields.length != 0) {
                        params.messageTest.value.warn(error.errorFields[0].errors[0])
                    }

                });
        }
    }
    const template = {
        test: {
            template: `<div @click="add">+</div> <div>{{testnum}}</div>`,

            setup() {

                return {
                    ...params,
                    ...methods,
                };
            }

        }
    }
    if (proxy) {

        params.proxy.value = proxy
    }

    //let template = toRefs(vtemplate)
    return { // 返回对象给模板

        ...params,
        ...methods,
        template

    }
}


export default Composition